package com.byd.dms.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.byd.dms.entity.Dept;
import com.byd.dms.entity.User;
import com.byd.dms.entity.User2;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.util.*;

public class FastDemo3_2 {
    private static final Dept deptProp;
    private static final User userProp;
    private static final User2 user2Prop;
    
    static {
        userProp = new User("刘阿勇", 18, "男");
        Dept dept2 = new Dept(5L, "测试部", 52);
        userProp.setDept(dept2);
        userProp.setHobbies(Arrays.asList("吃饭", "睡觉", "乌拉圈"));
        
        deptProp = new Dept(3L, "开发部", 38);
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        
        deptProp.setUsers(list);
        
        user2Prop = new User2("韩梅梅", 22, "女");
    }
    
    
    @Test
    public void listToMap() {
        user2Prop.getMap().add(new Dept(6L, "产品", 62));
        user2Prop.getMap().add(new Dept(7L, "运营", 32));
        String jsonStr = JSON.toJSONString(user2Prop);
        System.out.println("jsonStr = " + jsonStr);
        
        
        //解析时json将list转为map User2.map ===> User.map List --->  Map
        User user = JSON.parseObject(jsonStr, User.class);
        System.out.println("user = " + user);
    }
    
    @Test
    public void mapToList() throws JsonProcessingException {
        Map<Integer, Dept> map = new HashMap<>();
        map.put(0, new Dept(6L, "产品", 62));
        map.put(1, new Dept(7L, "运营", 32));
        userProp.setMap(map);
        String jsonStr = JSON.toJSONString(userProp);
        System.out.println("jsonStr = " + jsonStr);
        
        //JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user);
        //System.out.println("map: " + jsonObject1.getString("map"));
        
        //解析时json将map转为list User.map ===> User2.map  Map ---> List
        
        
        String str = "{\"age\":18,\"dept\":{\"deptId\":5,\"detpCount\":52,\"detpName\":\"测试部\"},\"hobbies\":[\"吃饭\"," +
                "\"睡觉\",\"乌拉圈\"],\"map\":[{\"deptId\":6,\"detpCount\":62,\"detpName\":\"产品\"},{\"deptId\":7,\"detpCount\":32,\"detpName\":\"运营\"}],\"name\":\"刘阿勇\",\"sex\":\"男\"}";
    
        String str2 = "{\"age\":18,\"dept\":{\"deptId\":5,\"detpCount\":52,\"detpName\":\"测试部\"},\"hobbies\":[\"吃饭\"," +
                "\"睡觉\"," +
                "\"乌拉圈\"],\"name\":\"刘阿勇\",\"sex\":\"男\"}";
        
        System.out.println(str);
        
        
        ObjectMapper objectMapper = new ObjectMapper();
        User2 testBean = objectMapper.readValue(jsonStr, User2.class);
        System.out.println("JackSon 解析: " + testBean);
        
        User2 user2 = JSON.parseObject(jsonStr, User2.class);
        System.out.println("fastjson解析: " + user2);
        
        User2 user21 = new User2("韩梅梅", 22, "女");
        System.out.println(JSON.toJSONString(user21));
        
    }
    
    @Test
    public void toMap() {
        String str = "{0:{\"detpName\":\"产品\",\"deptId\":6,\"detpCount\":62},1:{\"detpName\":\"运营\",\"deptId\":7," +
                "\"detpCount\":32}}";
         Map<Integer, Dept> integerDeptHashMap = JSON.parseObject(str, new TypeReference<HashMap<Integer, Dept>>(){});
        System.out.println("integerDeptHashMap = " + integerDeptHashMap);
    }
}
